package cn.wildfirechat.message;

import android.os.Parcel;
import android.text.TextUtils;

import org.json.JSONException;
import org.json.JSONObject;

import cn.wildfirechat.message.core.ContentTag;
import cn.wildfirechat.message.core.MessagePayload;
import cn.wildfirechat.message.core.PersistFlag;

import static cn.wildfirechat.message.core.MessageContentType.MESSAGE_TRANSFER_TYPE_FEED;

/**
 * 转账
 */

@ContentTag(type = MESSAGE_TRANSFER_TYPE_FEED, flag = PersistFlag.Persist_And_Count)
public class TransferMessageContent extends MessageContent {
    private String amount;
    private String memo;
    private String coinType;
    private String id;
    private String pushContent;


    public TransferMessageContent() {
    }


    @Override
    public MessagePayload encode() {
        MessagePayload payload = new MessagePayload();

        try {
            JSONObject objWrite = new JSONObject();
            objWrite.put("amount", amount);
            objWrite.put("coinType", coinType);
            objWrite.put("memo", memo);
            objWrite.put("id",id);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("order", objWrite);
            payload.searchableContent = pushContent;
            payload.content = jsonObject.toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return payload;
    }

    @Override
    public void decode(MessagePayload payload) {
        if (!TextUtils.isEmpty(payload.content)) {
            try {
                if (payload.content != null) {
                    JSONObject jsonObject = new JSONObject(payload.content);
                    pushContent = payload.searchableContent;
                    JSONObject order = jsonObject.optJSONObject("order");
                    amount = order.optString("amount");
                    coinType = order.optString("coinType");
                    memo = order.optString("memo");
                    id=order.optString("id");
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    protected TransferMessageContent(Parcel in) {
        super(in);
        this.amount = in.readString();
        this.coinType = in.readString();
        this.memo = in.readString();
        this.pushContent = in.readString();
        this.id=in.readString();
    }


    @Override
    public String digest(Message message) {

        return pushContent;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(this.amount);
        dest.writeString(this.coinType);
        dest.writeString(this.memo);
        dest.writeString(this.pushContent);
        dest.writeString(this.id);

    }

    public static final Creator<TransferMessageContent> CREATOR = new Creator<TransferMessageContent>() {
        @Override
        public TransferMessageContent createFromParcel(Parcel source) {
            return new TransferMessageContent(source);
        }

        @Override
        public TransferMessageContent[] newArray(int size) {
            return new TransferMessageContent[size];
        }
    };

    public String getAmount() {
        return amount;
    }

    public void setAmount(String amount) {
        this.amount = amount;
    }

    public String getMemo() {
        return memo;
    }

    public void setMemo(String memo) {
        this.memo = memo;
    }

    public String getCoinType() {
        return coinType;
    }

    public void setCoinType(String coinType) {
        this.coinType = coinType;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }
}
